#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import logging

from com.chenxiaojie.config import config
from com.chenxiaojie.dao.orm import create_pool
from com.chenxiaojie.model.blog import Blog
from com.chenxiaojie.model.reply import Reply
from com.chenxiaojie.model.user import User

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
logging.getLogger().setLevel(logging.INFO)


def insert(loop):
    user = User(name='chenxiaojie', image='new')
    print(loop.run_until_complete(user.insert()))
    blog = Blog(user_name='chenxiaojie', content='new')
    print(loop.run_until_complete(blog.insert()))
    reply = Reply(user_name='chenxiaojie', content='new')
    print(loop.run_until_complete(reply.insert()))


def insert2(loop):
    user = User(id='9c00e116b2c949a5803aec694f4c35e1', name='chenxiaojie', image='new')
    print(loop.run_until_complete(user.insert()))
    blog = Blog(id='e770bf1806cb43df8a913a4b0fa99b45', content='new')
    print(loop.run_until_complete(blog.insert()))
    reply = Reply(id='36b77fd66a5e4efa8eea4d9ed0ee5615', content='new')
    print(loop.run_until_complete(reply.insert()))


def update(loop):
    user = User(id='9c00e116b2c949a5803aec694f4c35e1', name='chenxiaojie', image='newnew')
    print(loop.run_until_complete(user.update()))
    blog = Blog(id='e770bf1806cb43df8a913a4b0fa99b45', content='newnew')
    print(loop.run_until_complete(blog.update()))
    reply = Reply(id='36b77fd66a5e4efa8eea4d9ed0ee5615', content='newnew')
    print(loop.run_until_complete(reply.update()))


def delete(loop):
    user = User(id='9c00e116b2c949a5803aec694f4c35e1', name='chenxiaojie', image='newnew')
    print(loop.run_until_complete(user.delete()))
    blog = Blog(id='e770bf1806cb43df8a913a4b0fa99b45', content='newnew')
    print(loop.run_until_complete(blog.delete()))
    reply = Reply(id='36b77fd66a5e4efa8eea4d9ed0ee5615', content='newnew')
    print(loop.run_until_complete(reply.delete()))


def find(loop):
    user = User(id='9c00e116b2c949a5803aec694f4c35e1', name='chenxiaojie', image='newnew')
    users = loop.run_until_complete(user.findAll())

    for u in users:
        print(u)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(create_pool(loop, **config.configs))
    # insert2(loop)
    # update(loop)
    # delete(loop)
    # insert(loop)
    find(loop)
